package com.sykj.exception;


import com.sykj.util.reponse.ResultBody;
import org.springframework.data.redis.RedisSystemException;
import org.springframework.validation.BindException;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;


/**
 * @program: BeautyClub
 * @description:全局异常处理类
 * @author: qiaoli
 * @create: 2020-07-15 09:21
 */

@RestControllerAdvice
public class MyExceptionHandler {


    @ExceptionHandler(BindException.class)
    public ResultBody MethodArgumentNotValidExceptionHandler(BindException e) {
        // 从异常对象中拿到ObjectError对象
        ObjectError objectError = e.getBindingResult().getAllErrors().get(0);
        // 然后提取错误提示信息进行返回
        return new ResultBody("10001", objectError.getDefaultMessage());
    }


    @ExceptionHandler(RedisSystemException.class)
    public ResultBody MethodArgumentNotValidExceptionHandler(RedisSystemException e) {
        // 从异常对象中拿到ObjectError对象
        String objectError = e.getLocalizedMessage();
        // 然后提取错误提示信息进行返回
        return new ResultBody("10001", objectError);
    }

}
